package nl.infodation.emr.pricingservice.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.data.Item;
import nl.infodation.emr.pricingservice.ui.component.IView;
import nl.infodation.emr.pricingservice.ui.component.PagingComponent;
import nl.infodation.emr.pricingservice.ui.component.PagingModel;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Button.ClickEvent;

public class LoadedRulesPopup extends CustomComponent implements IView, ItemClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private Label labelSecond;
	@AutoGenerated
	private Label label;
	@AutoGenerated
	private PagingComponent pagingComponent;
	@AutoGenerated
	private LoadedRulesList loadedRulesList;
	private PagingModel model = new PagingModel(0);

	private static final long serialVersionUID = 1L;
	
	private List<Item> items;
	
	public LoadedRulesPopup(List<Item> items) {
		this.items = items; 
		initLayout();
	}
	
	@Override
	public void initLayout() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		initialNumberOfRules();
		initialModelPaging();
	}
	
	private void initialNumberOfRules() {
		labelSecond.setValue(SessionData.getI18N().getMessage(
				Constants.NUMBER_OF_LOADED_RULES)
				+ items.size());
	}
	
	private void initialModelPaging() {
		loadedRulesList.setDataSource(loadItemsFromPage(1));
		setTotalNumberOfPagesForReload();

		model.addPropertyChangeListener("currentPage",
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumber();
						loadedRulesList.setDataSource(loadItemsFromPage(model.getCurrentPage()));
					}
				});
		pagingComponent.setModel(model);
		updatePageNumber();
	}
	
	private List<Item> loadItemsFromPage(int page) {
		List<Item> results = new ArrayList<Item>(); 
		int beginIndex = (page - 1) * loadedRulesList.getPageSize();
		int count = 0;
		while (beginIndex + count < items.size() && count < loadedRulesList.getPageSize()) {
			results.add(items.get(beginIndex + count));
			count++;
		}
		return results;
	}
	
	private void updatePageNumber() {
		label.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ model.getCurrentPage());
	}
	
	private void setTotalNumberOfPagesForReload() {
		
		final int totalPages = (int) Math.ceil(items.size()
				/ (float) loadedRulesList.getPageSize());
		model.setTotalNumberOfPages(totalPages);
	}
	
	@Override
	public void reload() {
	}

	@Override
	public void buttonClick(ClickEvent event) {
	}
	
	@Override
	public void itemClick(ItemClickEvent event) {
	}
	
	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(Boolean.FALSE);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(Boolean.FALSE);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// loadedRulesList_1
		loadedRulesList = new LoadedRulesList();
		loadedRulesList.setImmediate(Boolean.FALSE);
		loadedRulesList.setWidth("97.0%");
		loadedRulesList.setHeight("410px");
		mainLayout.addComponent(loadedRulesList, "top:0.0px;left:0.0px;");
		
		// pagingComponent_1
		pagingComponent = new PagingComponent();
		pagingComponent.setImmediate(Boolean.FALSE);
		pagingComponent.setWidth("-1px");
		pagingComponent.setHeight("-1px");
		mainLayout.addComponent(pagingComponent, "top:432.0px;left:0.0px;");
		
		// label_1
		label = new Label();
		label.setImmediate(Boolean.FALSE);
		label.setWidth("-1px");
		label.setHeight("-1px");
		label.setValue("Label");
		mainLayout.addComponent(label, "top:432.0px;left:529.0px;");
		
		// label_2
		labelSecond = new Label();
		labelSecond.setImmediate(Boolean.FALSE);
		labelSecond.setWidth("-1px");
		labelSecond.setHeight("-1px");
		labelSecond.setValue("Label");
		mainLayout.addComponent(labelSecond, "top:464.0px;left:529.0px;");
		
		return mainLayout;
	}
}

